package cn.cjxy.javaweb.servlet.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 取值
 *
 *  Cookie中使用特殊字符的情况
 * 在cookie值中使用分号（;）、逗号（,）、等号（=）、空格和中文等情况需要编码处理
 * 类与方法	说明
 * java.net.URLEncoder.encode("字符串","utf-8")	把字符串使用utf-8进行编码
 * java.net.URLDecoder.decode("字符串","utf-8")	把字符串使用utf-8进行解码
 * @author huangjie
 * @date 2025/9/29
 */
@WebServlet("/cookiedemo2")
public class CookieDemo2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //设置响应头
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
        //获取cookie
            Cookie[] cookies = req.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    writer.println("取出的cookie"+cookie.getName()+cookie.getValue());
                }
            }else {
                writer.println("cookie为空");
            }
    }
}
